package com.macro.mall.pms.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.StrSpliter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.exception.BusinessException;
import com.macro.mall.common.api.util.StringUtils;
import com.macro.mall.pms.domain.Attribute;
import com.macro.mall.pms.entity.AttributeEntity;
import com.macro.mall.pms.mapper.AttributeMapper;
import com.macro.mall.pms.service.AttributeService;
import com.macro.mall.pms.service.bo.InsertAttributeInput;
import com.macro.mall.pms.service.bo.PagedQueryAttributeInput;
import com.macro.mall.pms.service.bo.UpdateAttributeInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品属性参数Service
 *
 * @author xuyanjun
 * @date 2019-12-13
 */
@Slf4j(topic = "AttributeServiceImpl")
@Service
public class AttributeServiceImpl implements AttributeService {

    @Autowired
    private AttributeMapper attributeMapper;

    /**
     * 新增商品属性参数
     *
     * @param input input
     * @return 新增id
     */
    @Override
    public long insert(InsertAttributeInput input) {
        AttributeEntity attributeEntity = new AttributeEntity();
        attributeEntity
                .setProductAttributeCategoryId(input.getProductAttributeCategoryId())
                .setName(input.getName())
                .setSelectType(input.getSelectType())
                .setInputType(input.getInputType())
                .setInputList(input.getInputList())
                .setSort(input.getSort())
                .setFilterType(input.getFilterType())
                .setSearchType(input.getSearchType())
                .setRelatedStatus(input.getRelatedStatus())
                .setHandAddStatus(input.getHandAddStatus())
                .setType(input.getType());

        attributeMapper.insert(attributeEntity);
        return attributeEntity.getId();
    }

    /**
     * 修改商品属性参数
     *
     * @param input input
     */
    @Override
    public void update(UpdateAttributeInput input) {
        AttributeEntity attributeEntity = attributeMapper.selectById(input.getId());
        if (attributeEntity == null) {
            throw new BusinessException(String.format("未能根据Id:%s找到指定的商品属性参数。", input.getId()));
        }

        attributeEntity = new AttributeEntity();
        attributeEntity
                .setId(input.getId())
                .setProductAttributeCategoryId(input.getProductAttributeCategoryId())
                .setName(input.getName())
                .setSelectType(input.getSelectType())
                .setInputType(input.getInputType())
                .setInputList(input.getInputList())
                .setSort(input.getSort())
                .setFilterType(input.getFilterType())
                .setSearchType(input.getSearchType())
                .setRelatedStatus(input.getRelatedStatus())
                .setHandAddStatus(input.getHandAddStatus())
                .setType(input.getType());


        attributeMapper.updateById(attributeEntity);
    }

    /**
     * 删除商品属性参数
     *
     * @param id id
     */
    @Override
    public void delete(long id) {
        attributeMapper.deleteById(id);
    }

    /**
     * 根据Id获取商品属性参数
     *
     * @param id id
     * @return 商品属性参数
     */
    @Override
    public AttributeEntity getById(long id) {
        return attributeMapper.selectById(id);
    }

    /**
     * 根据属性类目id获取商品属性参数
     *
     * @param attributeCategoryId 属性类目id
     * @return 商品属性参数集合
     */
    @Override
    public List<AttributeEntity> listByAttributeCategoryId(long attributeCategoryId) {
        Assert.isTrue(attributeCategoryId > 0, "属性类目id不能为空。");

        LambdaQueryWrapper<AttributeEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AttributeEntity::getProductAttributeCategoryId, attributeCategoryId);

        return attributeMapper.selectList(queryWrapper);
    }

    /**
     * 根据批量Id获取商品属性参数
     *
     * @param batchIds 批量ids
     * @return 商品属性参数集合
     */
    @Override
    public List<AttributeEntity> listByBatchIds(List<Long> batchIds) {
        if (batchIds == null || batchIds.isEmpty()) {
            return new ArrayList<>();
        }
        return attributeMapper.selectBatchIds(batchIds);
    }

    /**
     * 分页查询
     *
     * @param input input
     * @return 分页的商品属性参数
     */
    @Override
    public Page<AttributeEntity> pagedQuery(PagedQueryAttributeInput input) {
        LambdaQueryWrapper<AttributeEntity> queryWrapper = Wrappers.lambdaQuery();

        PageHelper.startPage(input.getPageIndex(), input.getPageSize());

        return (Page<AttributeEntity>) attributeMapper.selectList(queryWrapper);
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param attributeEntity 实体
     * @return attribute
     */
    @Override
    public Attribute entityToDomain(AttributeEntity attributeEntity) {
        if (attributeEntity == null) {
            return null;
        }

        Attribute attribute = new Attribute();
        attribute
                .setId(attributeEntity.getId())
                .setProductAttributeCategoryId(attributeEntity.getProductAttributeCategoryId())
                .setName(attributeEntity.getName())
                .setSelectType(attributeEntity.getSelectType())
                .setInputType(attributeEntity.getInputType())
                .setSort(attributeEntity.getSort())
                .setFilterType(attributeEntity.getFilterType())
                .setSearchType(attributeEntity.getSearchType())
                .setRelatedStatus(attributeEntity.getRelatedStatus())
                .setHandAddStatus(attributeEntity.getHandAddStatus())
                .setType(attributeEntity.getType());

        if (StringUtils.isNotBlank(attributeEntity.getInputList())) {
            attribute.setInputList(StrSpliter.splitTrim(attributeEntity.getInputList(), ',', true));
        }

        return attribute;
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param attributeEntities 实体
     * @return attributes
     */
    @Override
    public List<Attribute> entityToDomain(List<AttributeEntity> attributeEntities) {
        List<Attribute> result = new ArrayList<>();
        if (attributeEntities == null) {
            return result;
        }

        for (AttributeEntity attributeEntity : attributeEntities) {
            Attribute attribute = entityToDomain(attributeEntity);
            result.add(attribute);
        }
        return result;
    }

}
